If large file support is enabled on 32 bit systems, it is possible
to trigger an out of boundary write with files larger than 2 GB.
Always check if fseek and ftell are successful and if the file is
small enough to fit into memory.
Signed-off-by: Tobias Stoeckmann <tobias@stoeckmann.org>
if (!file)
return -1;
- fseek (file, 0, SEEK_END);
- size = ftell (file);
+ if (fseek (file, 0, SEEK_END) == -1 || (size = ftell (file)) == -1)
+ {
+ fclose (file);
+ return -1;
+ }
if (length) *length = size;
rewind (file);
+ if ((size_t) size > SIZE_MAX - 8)
+ {
+ fclose (file);
+ return -1;
+ }
buffer = calloc(size + 8, 1);
if (!buffer)